package org.gpp.proj1.logic.metadata;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Represents a robot on the player's robot list
 * 
 * @author Paolo Victor, paolovictor@gmail.com
 */
public class RobotEntry implements Serializable {
	
	private static final long serialVersionUID = -8675281221044331184L;

	/**
	 * The robot's name
	 */
	private String robotName;
	
	/**
	 * The robot owner's name
	 */
	private String ownerName;

	/**
	 * Maps the robot's part categories and their
	 * respective part entries
	 */
	private Map<RobotPartCategory, PartEntry> partMapping;
	
	/**
	 * The robot's victories - The number of times it destroyed 
	 * another robot
	 */
	private int victories;
	
	/**
	 * The robot's losses - The number of times it was destroyed 
	 * by another robot
	 */
	private int losses;
	
	public RobotEntry( String ownerName, String robotName, Map<RobotPartCategory, PartEntry> partMapping, int victories, int losses ) {
		
		this.ownerName = ownerName;
		this.robotName = robotName;
		this.partMapping = partMapping;
		
		this.victories = victories;
		this.losses = losses;
	}
	
	public void addVictory() {
		victories++;
	}
	
	public void addLoss() {
		losses++;
	}

	public int getLosses() {
		return losses;
	}

	public String getOwnerName() {
		return ownerName;
	}

	public String getRobotName() {
		return robotName;
	}

	public int getVictories() {
		return victories;
	}
	
	/**
	 * @param partCategory
	 * @return The part file assigned to the parameter category
	 */
	public PartEntry getPart( RobotPartCategory partCategory ) {
		if( ! partMapping.containsKey( partCategory ) ) {
			return null;
		}
		
		return partMapping.get( partCategory );
	}

	/**
	 * Switches the current part assigned to the given part category
	 * for a new part file
	 * 
	 * @param robotPartCategory
	 * @param part 
	 */
	public void switchPart( RobotPartCategory robotPartCategory, PartEntry part ) {

		if( ! partMapping.containsKey( robotPartCategory ) ) {
			throw new IllegalArgumentException( "Invalid category" );
		}
		
		partMapping.remove( robotPartCategory );
		partMapping.put( robotPartCategory, part );
	}

	/**
	 * Copies a  part mapping to this robot entry
	 * @param newMapping
	 */
	public void copyMapping( Map<RobotPartCategory, PartEntry> newMapping) {

		this.partMapping = new HashMap<RobotPartCategory, PartEntry>( newMapping );
	}

	public String getDescription() {
		String description = "Name: " + this.getRobotName() + "\n";
		description += "Victories: " + this.getVictories() + "\n";
		description += "Losses: " + this.getLosses() + "\n";
		description += "\n";
		description += "Parts:\n";
		description += "  Head: " + this.partMapping.get( RobotPartCategory.HEAD ).getName() + "\n";
		description += "  Torso: " + this.partMapping.get( RobotPartCategory.TORSO ).getName() + "\n";
		description += "  Legs: " + this.partMapping.get( RobotPartCategory.LEGS ).getName() + "\n";
		description += "  Left Arm: " + this.partMapping.get( RobotPartCategory.ARM_LEFT ).getName() + "\n";
		description += "  Right Arm: " + this.partMapping.get( RobotPartCategory.ARM_RIGHT ).getName() + "\n";
		
		return description;
	}
}
